package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Member;
import com.itheima.pojo.User;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    @Override
    //根据月份查询对应月份会员数
    public List<Integer> findMemberCountByMonth(List<String> month) {
        //新建list集合用于存储每个月的最后一天日期对应的当月会员数
        List<Integer> list = new ArrayList<>();
        //遍历月份集合
        for (String m : month) {
            //月份加上日期，变成年月日的格式，加31
            m = m+".31";
            //根据月份最后一天查询本月共有会员数量
            Integer count =  memberDao.findMemberCountBeforeDate(m);
            //将会员数量存入会员数量集合
            list.add(count);
        }
        //返回会员数量集合
        return list;
    }

    //分页查询会员信息
    @Override
    public PageResult findPageMember(Integer currentPage, Integer pageSize, String queryString){
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> pageMember = memberDao.findPageMember(queryString);
        return new PageResult(pageMember.getTotal(),pageMember.getResult());
    }
    //添加会员信息
    @Override
    public void addMember(Member member) {
        System.out.println("member = " + member);
        memberDao.addMember(member);
    }

    //根据会员ID查询会员信息
    @Override
    public Member findMemberById(Integer id) {
        Member memberById = memberDao.findMemberById(id);
        return memberById;
    }

    //根据ID修改会员信息
    @Override
    public void updateMemberById(Member member) {
        System.out.println("member = " + member);
       memberDao.updateMemberById(member);
    }
    //根据会员ID删除会员信息
    @Override
    public void deleteMemberById(Integer id) {
        memberDao.deleteMemberById(id);
    }
    //获取所有会员信息
    @Override
    public List<Member> findAll(){
        return memberDao.findAll();
    }

    @Override
    public List<User> findUser() {
        return null;
    }

    //编辑会员信息
    @Override
    public void editMemberInfo(Member memberInfo) {
        memberDao.updateMemberById(memberInfo);
    }
    //获取当前用户的预约信息
    @Override
    public List<Map<String, Object>> findMemberOrderInfo(String startTime, String endTime,Integer memberId) {
        return memberDao.findMemberOrderInfo(startTime, endTime,memberId);
    }

    //获取当前用户的预约详情
    @Override
    public Map<String, Object> findMemberOrderDetail(Integer orderId) {
        return memberDao.findMemberOrderDetail(orderId);
    }

}
